import * as Utils from "@/MUI/utils/Function";
const files = require.context(".", true, /\.js/);
const store = Utils.exportFileAsObject(files);

import Store from "@/store";
import {
  getEntpriseList,
  getUserInfo,
  login,
  setCurrentEnterprise,
} from "@/request/common";
import { logout } from "@/request/common";
import { $m } from "@/MUI/utils";
import $config from "@/config";
import { win } from "codemirror/src/util/dom";

const state = {
  userInfo: null,
  enterprise: {
    list: [],
    current: null,
  },
  loading: {
    visible: true,
    text: "加载中...",
  },
};

const getters = {};

const mutations = {};

const actions = {
  // 项目初始化
  init: ({ state }, data) => {
    $m.sy({
      content: "",
    });
    return new Promise((resolve, reject) => {
      Store.dispatch("authInit", false);
      resolve();
    });
  },
  // 登录
  login: ({ state }, code) => {
    return new Promise((resolve, reject) => {
      login({
        code: code,
      })
        .then((res) => {
          resolve();
        })
        .catch((err) => {
          reject();
        });
    });
  },
  // 退出登录
  logout: ({ state }) => {
    Store.dispatch("setLoading", {
      visible: true,
      text: "正在退出登录...",
    });
    let logoutEnd = () => {
      setTimeout(() => {
        Store.dispatch("setLoading", {
          visible: false,
        });
        $m.to("Login");
        // window.location.reload();
      }, 1000);
    };

    logoutEnd();

    // logout()
    //   .then((res) => {
    //     console.log("logout", res);
    //     logoutEnd();
    //   })
    //   .catch((err) => {
    //     logoutEnd();
    //   });
  },
  // 设置全局loading
  setLoading: ({ state }, data) => {
    state.loading.text = data.text || "加载中...";
    setTimeout(() => {
      state.loading.visible = data.visible;
    }, 300);
  },
  // 跳转默认页面
  toDefaultPage({ state }, data) {
    $m.to("Home");
    if (state.userInfo && state.userInfo.defaultPage === "kanban") {
      var url = $config.dataV.url + "/#/middle/middlePlatform";
      window.location.replace(url);
    }
  },
};

export default {
  state,
  getters,
  mutations,
  actions,
  modules: {
    ...store,
  },
};
